package com.information.manage.gyjc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.gyjc.mapper.GongxuMapper;
import com.information.manage.gyjc.model.enity.Gongxu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/gyjc/gongxu")
public class GongxuController {
    @Autowired
    private GongxuMapper gongxuMapper;

    //查询工序信息列表
    @GetMapping("/list/{curPage}/{size}")
    public Result selectGongxuList(@PathVariable Integer curPage, @PathVariable Integer size) {
        Page<Gongxu> page = gongxuMapper.selectPage(new Page<>(curPage, size), null);
        return Result.ok(page);
    }

    //查询工序信息列表
    @GetMapping("/listAll")
    public Result listAll() {
        List<Gongxu> page = gongxuMapper.selectList(null);
        return Result.ok(page);
    }

    //查询工序信息
    @GetMapping("/select/{id}")
    public Result<Gongxu> selectGongxuById(@PathVariable Integer id) {
        return Result.ok(gongxuMapper.selectById(id));
    }

    //删除工序信息
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteGongxuById(@PathVariable Integer id) {
        int res = gongxuMapper.deleteById(id);
        if (res > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    //添加工序信息
    @PostMapping("/add")
    public Result<String> insertGongxu(@RequestBody Gongxu gongxu) {
        int res = gongxuMapper.insert(gongxu);
        if (res > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    //修改工序信息
    @PostMapping("/update")
    public Result<String> updateGongxu(@RequestBody Gongxu gongxu) {
        int res = gongxuMapper.updateById(gongxu);
        if (res > 0) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }
}
